const { ListToNode, NodeToList, ListNode, timeL } = require("../utils.js");
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
  let list = [nums[0]],
    n = nums[0];
  for (let i = 1; i < nums.length; i++) {
    n = Math.max(nums[i], n);
    let newValue = list[list.length - 1];
    if ((nums[i] < 0 && newValue < 0) || (nums[i] >= 0 && newValue >= 0)) {
      list[list.length - 1] += nums[i];
    } else {
      list.push(nums[i]);
    }
  }
  let arr = [list[0]];
  n = Math.max(list[0], n);
  for (let i = 1; i < list.length; i++) {
    arr = arr.map((item) => {
      let value = item + list[i];
      if (value > n) n = value;
      return value;
    });
    arr.push(list[i]);
    arr = arr.filter((item) => item > 0);
    n = list[i] > n ? list[i] : n;
  }
  return n;
};
let nums = [-1, -2];
timeL(maxSubArray, [nums]);
